﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MMOShared;
using System.Security.AccessControl;
using MMOResCreator;

namespace MMOCustomControls
{
    public partial class EditableListView : ListView
    {
        public delegate void ResourceEntryUpdated(int index, ResourceEntry updatedEntry);
        public event ResourceEntryUpdated OnResourceEntryUpdated;

        private ListViewItem li;
        private int X = 0;
        private int Y = 0;
        private string subItemText;
        private int subItemSelected = 0;
        private System.Windows.Forms.TextBox editBox = new System.Windows.Forms.TextBox();
        
        private System.Windows.Forms.ColumnHeader columnHeader1;
        private System.Windows.Forms.ColumnHeader columnHeader2;
        private System.Windows.Forms.ColumnHeader columnHeader3;
        private System.Windows.Forms.ColumnHeader columnHeader4;

        public EditableListView()
        {
            this.CheckBoxes = true;

            this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader4 = new System.Windows.Forms.ColumnHeader();

            this.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																			this.columnHeader1,
																			this.columnHeader2,
																			this.columnHeader3,
																			this.columnHeader4});
            this.Dock = System.Windows.Forms.DockStyle.Fill;
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.FullRowSelect = true;
            this.Name = "resL";
            this.Size = new System.Drawing.Size(0, 0);
            this.MultiSelect = false;
            this.TabIndex = 0;
            this.View = System.Windows.Forms.View.Details;
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.SMKMouseDown);
            this.DoubleClick += new System.EventHandler(this.SMKDoubleClick);
            this.GridLines = true;
            // 
            // columnHeader1
            // 
            this.columnHeader1.Text = "Resource ID";
            this.columnHeader1.Width = 163;
            // 
            // columnHeader2
            // 
            this.columnHeader2.Text = "Relative Path";
            this.columnHeader2.Width = 225;
            // 
            // columnHeader3
            // 
            this.columnHeader3.Text = "Content Path";
            this.columnHeader3.Width = 225;
            // 
            // columnHeader4
            // 
            this.columnHeader4.Text = "CheckSum";
            this.columnHeader4.Width = 400;

            editBox.Size = new System.Drawing.Size(0, 0);
            editBox.Location = new System.Drawing.Point(0, 0);
            this.Controls.AddRange(new System.Windows.Forms.Control[] { this.editBox });
            editBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.EditOver);
            editBox.LostFocus += new System.EventHandler(this.FocusOver);
            editBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            editBox.BackColor = Color.LightYellow;
            editBox.BorderStyle = BorderStyle.Fixed3D;
            editBox.Hide();
            editBox.Text = "";
        }

        private void EditOver(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                li.SubItems[subItemSelected].Text = editBox.Text;
                editBox.Hide();
            }

            if (e.KeyChar == 27)
                editBox.Hide();
        }

        private void FocusOver(object sender, System.EventArgs e)
        {
            li.SubItems[subItemSelected].Text = editBox.Text;
            editBox.Hide();

            int row = li.Index;
            string[] values = new string[li.SubItems.Count];
            for (int i = 0; i < li.SubItems.Count; i++)
            {
                values[i] = li.SubItems[i].Text;
            }
            ResourceEntry updatedEntry = new ResourceEntry(values[1], values[2], values[0], values[3],CompileOptions.NO_COMPILE);
            OnResourceEntryUpdated(row, updatedEntry);
        }

        public void SMKDoubleClick(object sender, System.EventArgs e)
        {
            // Check the subitem clicked .
            int nStart = X;
            int spos = 0;
            int epos = this.Columns[0].Width;
            for (int i = 0; i < this.Columns.Count; i++)
            {
                if (nStart > spos && nStart < epos)
                {
                    subItemSelected = i;
                    break;
                }

                spos = epos;
                epos += this.Columns[i].Width;
            }

            Console.WriteLine("SUB ITEM SELECTED = " + li.SubItems[subItemSelected].Text);
            subItemText = li.SubItems[subItemSelected].Text;

            string colName = this.Columns[subItemSelected].Text;
            if (colName != "CheckSum")
            {
                Rectangle r = new Rectangle(spos, li.Bounds.Y, epos, li.Bounds.Bottom);
                editBox.Size = new System.Drawing.Size(epos - spos, li.Bounds.Bottom - li.Bounds.Top);
                editBox.Location = new System.Drawing.Point(spos, li.Bounds.Y);
                editBox.Show();
                editBox.Text = subItemText;
                editBox.SelectAll();
                editBox.Focus();
                editBox.Tag = li.Index;
            }
        }

        public void SMKMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            li = this.GetItemAt(e.X, e.Y);
            X = e.X;
            Y = e.Y;
        }

        public void SetResourceItems(List<ResourceEntry> entries,string basePath)
        {
            this.Items.Clear();
            for (int i = 0; i < entries.Count; i++)
            {
                ResourceEntry e = entries[i];
                string[] s = new string[4];
                s[0] = e.ResourceID;
                s[1] = e.RelativePath;
                s[2] = e.ContentPath;
                s[3] = e.CheckSum;
                this.Items.Add(new ListViewItem(s));
                MMOResCreator.ResourceType t = CompileResourceEntry.GetResourceType(basePath+e.RelativePath);
                if (t != MMOResCreator.ResourceType.RES_OTHER)
                {
                    this.Items[i].Checked = true;
                }
            }
        }

        public void AddNewItem(string resId, string relP, string contP, string chkSum, string fullPath)
        {
            string[] s = new string[4];
            s[0] = resId;
            s[1] = relP;
            s[2] = contP;
            s[3] = chkSum;
            this.Items.Add(new ListViewItem(s));
            MMOResCreator.ResourceType t = CompileResourceEntry.GetResourceType(fullPath);
            if (t != MMOResCreator.ResourceType.RES_OTHER)
            {
                this.Items[this.Items.Count - 1].Checked = true;
            }
        }

    }
}